#!/bin/sh
#
# Slackware build script for RTL8192 wifi USB driver.
#
# Copyright 2020 by Jay, Seattle, WA.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=RTL8192
VERSION=${VERSION:-5.2.19.1}
BUILD=${BUILD:-25633}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i386 ;;
  arm64*) ARCH=arm64 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
else
  # 32-bit PC build fails if ARCH != i386
  case "$ARCH" in
    i?86) export ARCH=i386 ;;
  esac
fi

# Caveat emptor
case "$ARCH" in
    i386) ;;
  x86_64) ;;
       *) echo -e "\nBuild for $ARCH is untested!!"
          echo -e "Press Ctrl-C within ten seconds to abort,"
          echo -e "or press ENTER to continue at your own risk.\n"
          read -t 10 PLACEHOLDER ;;
esac

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}
KVER=${KVER:-$( uname -r )}

set -e

rm -rf $PKG
rm -rf $TMP/$PRGNAM
mkdir -p $PKG $OUTPUT $TMP/$PRGNAM
# Nested zip files
unzip $CWD/TP-Link_Driver_Linux_series8_beta.zip -d $TMP/$PRGNAM
cd $TMP/$PRGNAM
unzip rtl8192EU_WiFi_linux_v5.2.19.1_25633.20171222_COEX20171113-0047.zip
cd rtl8192EU_WiFi_linux_v5.2.19.1_25633.20171222_COEX20171113-0047

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

mkdir -p $PKG/lib/modules/$KVER/kernel/drivers/net/wireless/
sed -i 's/install -p -m 644 $(MODULE_NAME).ko  $(MODDESTDIR)/install -p -m 644 $(MODULE_NAME).ko  $(DESTDIR)$(MODDESTDIR)/' Makefile
sed -i "s/\/sbin\/depmod -a \${KVER}//" Makefile
sed -i "s/KVER.*:=/KVER ?=/" Makefile

make
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $TMP/$PRGNAM/readme_beta.txt > $PKG/usr/doc/$PRGNAM-$VERSION/readme_beta.txt
cp $TMP/$PRGNAM/*.pdf $PKG/usr/doc/$PRGNAM-$VERSION/

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat << EOF > $PKG/install/doinst.sh
# A good idea whenever kernel modules are added or changed:
if [ -x sbin/depmod ]; then
  chroot . /sbin/depmod -a 1> /dev/null 2> /dev/null
fi
EOF

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$KVER-$BUILD$TAG.${PKGTYPE:-tgz}
